<?php
    header('Access-Control-Allow-Origin:*');
    header("content-type:text/html;charset=utf-8");
    $servername = "localhost";
    $dbname = "sx_control";
    $db_username = "root";
    $db_password = "";
    $product_list = array();
    $user_list = array();
    $cotroller_list = array();
    if($_GET["table"] == "user") {
        try {
            $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $db_username, $db_password,array
            (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"));
            foreach ($dbh->query('SELECT * from user') as $row) { 
                array_push($user_list,$row);
            }
            echo json_encode($user_list,JSON_UNESCAPED_UNICODE);
            $dbh = null;
        } catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    } elseif ($_GET["table"] == "project") {
        try {
            $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $db_username, $db_password,array
            (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"));
            foreach ($dbh->query('SELECT * from project') as $row) {
                array_push($product_list,$row);
            }
            echo json_encode($product_list,JSON_UNESCAPED_UNICODE);
            $dbh = null;
        } catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }  elseif (stripos("controller",$_GET["table"]) >= 0) {
        try {
            $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $db_username, $db_password,array
            (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"));
            $sql = 'SELECT * from '.$_GET["table"]; 
            // print_r  ($sql);
            foreach ($dbh->query($sql) as $row) {
                // $data = $row->fetchAll(PDO::FETCH_ASSOC);  //如何只获取关联数组
                array_push($cotroller_list,$row);
            }
            echo json_encode($cotroller_list,JSON_UNESCAPED_UNICODE);
            $dbh = null;
        } catch(PDOException $e)
        {
            echo $e->getMessage();
        }    
    }
?>